﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace AllPurposeCard.Shenjiahao
{
    public partial class xiaofei : Form
    {
        //全局变量
        public DataTable ds = new DataTable();
        /// <summary>
        /// 传入一个字符串
        /// </summary>
        /// <returns>返回一个true和false</returns>
        public bool sjk(string a)
        {
            SqlConnection sqq = new SqlConnection("Data Source=.;Initial Catalog=AllPurposeCard;Integrated Security=True");
            sqq.Open();
            string sql = string.Format("select *from UserInformation where UserCardID='{0}'",a);
            SqlCommand con = new SqlCommand(sql, sqq);
            SqlDataReader data = con.ExecuteReader();
            data.Read();
            if (data.HasRows)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public xiaofei()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void xiaofei_Load(object sender, EventArgs e)
        {
            string sql = @"select e.detail,u.Date,e.operator,e.amount from ExpenseCalenda e, UserInformation u
                  where e.UserCardID = u.UserCardID";
            Cun(sql);
            
        }

       

        private void chaxun_btn_Click(object sender, EventArgs e)
        {

            string sql = string.Format(@"select u.UserName,u.UserSex,u.UserAge from UserInformation u,ExpenseCalenda e where u.UserCardID = e.UserCardID and e.UserCardID = '{0}'", this.textBox1.Text.Trim());
            Cha(sql);
            bool b=sjk(this.textBox1.Text);
            //连接数据库
            if (this.textBox1.Text != "")
            {
                if (b)
                {
                    this.Myname.Text = ds.Rows[0]["UserName"].ToString();
                    this.Sex.Text = ds.Rows[0]["UserSex"].ToString();
                    this.Age.Text = ds.Rows[0]["UserAge"].ToString();
                }
                else
                {
                    MessageBox.Show("查无此人");
                }
            }
            else
            {
                MessageBox.Show("卡号不能为空");
            }
            //查询
            
            

        }

        private void cz_btn_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=AllPurposeCard;Integrated Security=True");
            con.Open();
            string sql = string.Format(@"update UserInformation set Balance=Balance+{0}
                    where UserCardID = '{1}'", this.Money.Text, this.textBox1.Text);
            SqlCommand cmd = new SqlCommand(sql, con);
            if (cmd.ExecuteNonQuery()>0)
            {
                MessageBox.Show("充值成功");
            }
            else
            {
                MessageBox.Show("充值失败");
            }
        }
        private void chaxun_Click(object sender, EventArgs e)
        {
           
            string sql = string.Format(@"select e.detail,u.Date,e.operator,e.amount from ExpenseCalenda e, UserInformation u
              where e.UserCardID=u.UserCardID and u.UserCardID='{0}'", this.card_Y.Text.Trim());
            Cun(sql);
            string sql2 = string.Format(@"select Balance from UserInformation 
                where UserCardID = '{0}'",this.card_Y.Text.Trim());
            Cha(sql2);
            this.Yue.Text = ds.Rows[0]["Balance"].ToString();
        }
        public void Cun(string sql)
        {
            SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=AllPurposeCard;Integrated Security=True");
            
            SqlDataAdapter da = new SqlDataAdapter(sql, con);
            DataTable dt = new DataTable();
            dt.Clear();
            da.Fill(dt);
            this.dataGridView1.AllowUserToAddRows = false;
            this.dataGridView1.DataSource = dt;
        }
        public void Cha(string sql)
        {
            SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=AllPurposeCard;Integrated Security=True");
            SqlDataAdapter da = new SqlDataAdapter(sql, con);
            
            da.Fill(ds);
            
        }

       
    }
}
